<?php

$host = 'http://127.0.0.1:7002/';

// 商品列表
/* $url = $host . 'storeGoods/getList?scKey=1&page=1&perPage=10';
$data['money'] = 10; */
// 内销商品添加
$url = $host . 'storeInnerSales/getList?scKey=2';
$data['scKey'] = 1;


$rst = https_request($url ,$data);
//$rst = https_request($url ,$data, 'get');
// $rst = https_request_json($url ,$data); 

// 如果非json，直接显示后台错误
if(is_null(json_decode($rst))){
	echo $rst;exit;
}

echo "<pre>";
var_dump($data);
echo "</pre>";
echo $url . '<br>';
echo "<pre>";
echo "<textarea style='width:50%;height:750px;' id='json' onclick='copy()'>$rst</textarea>";
echo "<textarea style='width:50%;height:750px;' id='json2'>$rst</textarea>";
echo "</pre>";
echo "<pre>";
var_dump(json_decode($rst, true));
echo "</pre>";

function https_request_json($url, $data){
	$data_string = json_encode($data);
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json; charset=utf-8',
        'Content-Length: ' . strlen($data_string))
    );

	$result = curl_exec($ch);
	if (curl_errno($ch)) {
		print curl_error($ch);
	}
	curl_close($ch);
	return $result;
}

function https_request($url, $data = null, $type = 'post')
{
    $curl = curl_init();
	if (!empty($data) && $type == 'post') {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }else{
		if(!empty($data)){
			$query = '';
			foreach($data as $k => $v){
				$query .= $k . '=' . $v . '&';
			}
			$url = $url . "?" . $query;
		}echo $url;
	}
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

    
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

?>
<script>
// 一键复制
function copy(){
	var obj = document.getElementById("json");   
	obj.select(); // 选择对象
	document.execCommand("Copy");
}
// 格式json数据
var text = document.getElementById('json').value; //获取json格式内容
var result = JSON.stringify(JSON.parse(text), null, 2);//将字符串转换成json对象
document.getElementById('json').value= result ;

// 格式json数据
var text = document.getElementById('json2').value; //获取json格式内容
var result = JSON.stringify(JSON.parse(text), null, 2);//将字符串转换成json对象
document.getElementById('json2').value= result ;
</script>
 
 